# -*- coding: utf-8 -*-

import sys
import os
from SettingLoader import Setting
from PrintLogger import setup_print_log
from multiprocessing import Process, freeze_support
import time
# print 重定向
setup_print_log()

# 加载全局配置
global_setting = Setting().setting

def run_process(args, scripe_name='自动刷新空调.py'):
    '''
    启动购买脚本进行购买
    '''
    args = ' '.join(args)
    cmd = f'python ./{scripe_name} {args}'
    print(cmd)
    os.system(cmd)


if __name__ == '__main__':
    # 多进程支持
    freeze_support()
    # 解析全局配置
    print(global_setting)
    filter_run_list = global_setting['filter_run']
    for filter in filter_run_list:
        if filter["is_run"]:
            args=[]
            air_conditioning_type = filter['global_air_conditioning_type']
            args.append(f"global_air_conditioning_type={air_conditioning_type}")
            horse_power = filter['global_horse_power']
            args.append(f"global_horse_power={horse_power}")
            alarm_price = filter['global_alarm_price']
            args.append(f"global_alarm_price={alarm_price}")
            is_auto_run_lessprice = int(filter['global_is_auto_run_lessprice'])
            args.append(f"global_is_auto_run_lessprice={is_auto_run_lessprice}")
            p_one = Process(target=run_process, args=(args,))
            p_one.start()
            time.sleep(12)
